
class WebSocketClient 
{
    constructor(connect) 
    {
        this.mConnect = connect
    }

    destroy()
    {
        this.close()

        this.mConnect = null
    }

    close() 
    {
        this.clearTimer()
        
        if (this.mSocket) 
        {
            let d = this.mSocket

            this.mSocket = null

            d.close()
        }
    }

    connect(url, onSuccess, onMessage, onClose, onError) 
    {
        this.mURL = url

        this.createWs(onSuccess, onMessage, onClose, onError)

        this.reconnect = (callback)=>{

            this.close()

            this.createWs(callback || onSuccess, onMessage, onClose, onError, true)
        }
    }

    createWs(onOpen, onMessage, onClose, onError, isReconnect) 
    {
        Log.d("WebSocketClient", " open connect: " + this.mURL)

        let socket = new WebSocket(this.mURL)

        this.mSocket  = socket

        socket.onopen = (event) =>{

                        if (this.mSocket === socket)
                        {
                            onOpen(isReconnect)
                        
                            Log.d("WebSocketClient", " open connect ok : " + this.mURL)
                        }
                    }

        socket.onmessage = (event) => {

                        //if (this.mSocket === socket)
                        if (event)
                        {
                            onMessage(this.mConnect, event.data)
                        }
                    }

        socket.onerror = (event) =>{

                        Log.w('WebSocketClient', "on error, code: " + (event.code || 'null') + ', reason: ' + (event.reason || 'null'))

                        if (this.mSocket === socket)
                        {
                            this.close()

                            onError()
                        }
                    }
        
        socket.onclose = (event) =>{

                    Log.w('WebSocketClient', "on close, code: " + (event.code || 'null') + ', reason: ' + (event.reason || 'null'))

                    if (this.mSocket === socket)
                    {
                        this.close()

                        onClose(this)
                    }
                    else
                    {
                        Log.w('WebSocketClient', "not current socket, on close.")
                    }
                }
        
        this.clearTimer()

        this.mTimer = setTimeout(
            () =>{

                if (socket.readyState != WebSocket.OPEN) 
                {
                    Log.w('WebSocketClient', "connect time out: " + this.mURL)

                    if (this.mSocket === socket)
                    {
                        this.close()

                        onClose(this)
                    }
                    else
                    {
                        Log.w('WebSocketClient', "not current socket, on time out.")
                    }
                }
            }, 
            3000
        )
    }

    clearTimer()
    {
        if (this.mTimer)
        {
            clearTimeout(this.mTimer)
            this.mTimer = null
        }
    }

    send(data) 
    {
        if (!this.mSocket) 
        {
            Log.e("WebSocketClient", "socket null.")

            return false
        }

        if (this.mSocket.readyState != 1) 
        {
            Log.e("WebSocketClient", "socket is not open state.")

            return false
        }

        this.mSocket.send(data)

        //Log.d('WebSocketClient', "send data: " , data)

        return true
    }

    url() 
    {
        return this.mURL
    }
}

export default WebSocketClient
